import { updateLang } from "./api/service/user";

export const langList = ref([] as Array<any>);
const lang = import.meta.env.VITE_LANG;
let rootUrl = (globalThis as any)["BASE_URL"];
let baseUrl = rootUrl || import.meta.env.VITE_BASEURL;
export const msgData = ref();
export const setLang = async (lang: string) => {
  const url = `${baseUrl}/profile/language/${lang}.json`;
  localStorage.setItem("lang", lang);
  const getLang = await axios.get(url);
  const { data } = getLang;
  msgData.value = data;
};

export const updateUserLang = async (lang: string) => {
  await updateLang(lang);
  await setLang(lang);
  window.location.reload();
};
/* 获取语言列表 */
export const getLangList = async () => {
  const mapUrl = `${baseUrl}/profile/language/language.json`;
  const langConfig = await axios.get<any>(mapUrl);
  const { data } = langConfig.data;
  langList.value = data;
  const language = localStorage.getItem("lang") || lang;
  await setLang(language);

  return data;
};
